package com.leyou.service.impl;

import com.leyou.dto.*;
import com.leyou.item.client.ItemClient;
import com.leyou.myException.LyException;
import com.leyou.service.GoodsPageService;
import com.leyou.utils.JsonUtils;
import com.leyou.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author jshstart
 * @creat 2020-12-17 22:21
 */
@Service
public class GoodsPageServiceImpl implements GoodsPageService {
    @Autowired
    private ItemClient itemClient;
    @Autowired
    private StringRedisTemplate redisTemplate;
    private static final String SPU_KEY_PREFIX = "page:spu:id:";
    private static final String DETAIL_KEY_PREFIX = "page:detail:id:";
    private static final String SKU_KEY_PREFIX = "page:sku:id:";
    private static final String CATEGORY_KEY_PREFIX = "page:category:id:";
    private static final String BRAND_KEY_PREFIX = "page:brand:id:";
    private static final String SPEC_KEY_PREFIX = "page:spec:id:";

    /*
    加载spu数据到redis中
     */
    @Override
    public String loadSpuData(Long spuId) {
        SpuDTO spuDTO = itemClient.querySpuById(spuId);
        if (spuDTO != null && spuDTO.getId() != null) {
            SpuVO spuVO = spuDTO.toEntity(SpuVO.class);
            //。。。。。。
            spuVO.setCategoryIds(spuDTO.getCategoryIds());
            String spuJson = JsonUtils.toJson(spuVO);
            redisTemplate.opsForValue().set(SPU_KEY_PREFIX + spuId, spuJson);
            return spuJson;
        } else {
            throw new LyException(400, "找不到指定的spu");
        }
    }

    /*
        加载detail数据到redis中
         */
    @Override
    public String loadSpuDetailData(Long spuId) {
        SpuDetailDTO detailDTO = itemClient.querySpuDetailById(spuId);
        if (detailDTO != null && detailDTO.getSpuId() != null) {
            String detailJson = JsonUtils.toJson(detailDTO);
            redisTemplate.opsForValue().set(DETAIL_KEY_PREFIX + spuId, detailJson);
            return detailJson;
        } else {
            throw new LyException(400, "找不到指定的detail");
        }
    }

    /*
        加载sku数据到redis中
         */
    @Override
    public String loadSkuListData(Long spuId) {
        List<SkuDTO> skuDTOList = itemClient.querySkuBySpuId(spuId);
        if (skuDTOList != null && skuDTOList.size() > 0) {
            String skuJson = JsonUtils.toJson(skuDTOList);
            redisTemplate.opsForValue().set(SKU_KEY_PREFIX + spuId, skuJson);
            return skuJson;
        } else {
            throw new LyException(400, "找不到指定的sku");
        }
    }

    /*
        加载category数据到redis中
         */
    @Override
    public String loadCategoriesData(List<Long> ids) {
        List<CategoryDTO> categoryDTOList = itemClient.queryCategoryByIds(ids);
        if (categoryDTOList != null && categoryDTOList.size() > 0) {
            List<CategoryVO> categoryVOList = categoryDTOList.stream().map(categoryDTO -> categoryDTO.toEntity(CategoryVO.class)).collect(Collectors.toList());
            String categoryJson = JsonUtils.toJson(categoryVOList);
            redisTemplate.opsForValue().set(CATEGORY_KEY_PREFIX + ids.get(2), categoryJson);
            return categoryJson;
        } else {
            throw new LyException(400, "找不到指定的spu");
        }

    }

    /*
        加载brand数据到redis中
         */
    @Override
    public String loadBrandData(Long brandId) {
        BrandDTO brandDTO = itemClient.queryBrandById(brandId);
        if (brandDTO != null && brandDTO.getId() != null) {
            BrandVO brandVO = brandDTO.toEntity(BrandVO.class);
            String brandJson = JsonUtils.toJson(brandVO);
            redisTemplate.opsForValue().set(BRAND_KEY_PREFIX + brandId, brandJson);
            return brandJson;
        } else {
            throw new LyException(400, "找不到指定的spu");
        }
    }

    /*
        加载规格信息数据到redis中
         */
    @Override
    public String loadSpecData(Long categoryId) {
        // 1.根据分类id查询规格组及组内参数
//        List<SpecGroupDTO> groupDTOList = itemClient.querySpecList(categoryId);
//        if (groupDTOList==null||groupDTOList.size()<=0){
//            throw new LyException(400, "找不到指定的spu");
//        }
//        ArrayList<SpecGroupVO> groupVOList = new ArrayList<>(groupDTOList.size());
//        for (SpecGroupDTO specGroupDTO : groupDTOList) {
//            SpecGroupVO groupVO = new SpecGroupVO();
//            groupVO.setName(specGroupDTO.getName());
//            List<SpecParamVO> paramVOList = groupDTOList.stream().map(paramDto -> paramDto.toEntity(SpecParamVO.class)).collect(Collectors.toList());
//            groupVO.setParams(paramVOList);
//            groupVOList.add(groupVO);
//        }
//        String json = JsonUtils.toJson(groupVOList);
//        redisTemplate.opsForValue().set(SPEC_KEY_PREFIX+categoryId,json);
//        return json;
        // 1.根据分类id查询规格组及组内参数
        List<SpecGroupDTO> specGroups = itemClient.querySpecList(categoryId);
        if (specGroups == null || specGroups.size() <= 0) {
            throw new LyException(404, "商品不存在！");
        }
        // 2.转换VO
        List<SpecGroupVO> groupVOList = new ArrayList<>(specGroups.size());
        for (SpecGroupDTO specGroup : specGroups) {
            // 2.1.创建VO
            SpecGroupVO groupVO = new SpecGroupVO();
            // 2.2.放入集合
            groupVOList.add(groupVO);
            // 2.3.填写属性
            // name属性
            groupVO.setName(specGroup.getName());
            // params属性
            List<SpecParamVO> params = specGroup.getParams()
                    .stream()
                    .map(paramDTO -> paramDTO.toEntity(SpecParamVO.class))
                    .collect(Collectors.toList());
            groupVO.setParams(params);
        }
        // 3.序列化为json
        String json = JsonUtils.toJson(groupVOList);
        // 4.写入redis
        redisTemplate.opsForValue().set(SPEC_KEY_PREFIX + categoryId, json);
        // 5.返回
        return json;
    }

    @Override
    public void deleteSkuBySpuId(Long spuId) {
        redisTemplate.delete(SKU_KEY_PREFIX+spuId);
    }
}
